//
//  PYImageCountButton.m
//  PictureYourself
//
//  Created by Daniel on 4/8/12.
//
//

#import "PYImageCountButton.h"

@implementation PYImageCountButton

//@synthesize imageCount = imageCount_;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        imageCount_ = 0;
    }
    return self;
}

-(void)setImageCount:(int)imageCountLocal
{
    imageCount_ = imageCountLocal;
    [self setNeedsDisplay];
}

-(int)imageCount
{
    return imageCount_;
}

-(void)drawPictureStroke:(CGContextRef)context :(CGRect)bounds
{
    CGMutablePathRef path = CreateRoundRectPath(context, bounds);
    CGContextSetLineWidth(context, 1.5);
    CGContextStrokePath(context);
    CGPathRelease(path);
}

- (void)drawRect:(CGRect)rect
{
    CGRect bounds = rect;
    bounds.size.width *= 0.75;
    bounds.size.height = bounds.size.width / 1.66666666666667;
    bounds.origin.x = rect.size.width - bounds.size.width - 2;
    bounds.origin.y = rect.size.height - bounds.size.height - 2;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGFloat strokeOffset = -(bounds.size.width * 0.085);
    CGContextSetShadow(context, CGSizeMake(1.0, 1.0), 1.5);
    
    for(int i = 0; i < 3; i++)
    {
        if(i < 2)
        {
            [self drawPictureStroke:context :bounds];
            bounds.origin.x += strokeOffset;
            bounds.origin.y += strokeOffset;
        }
        else
        {
            CGRect fillBounds = bounds;
            
            fillBounds.origin.x += 1.0;
            fillBounds.origin.y += 1.0;
            //fillBounds.size.width -= 1.0;
            //fillBounds.size.height -= 1.0;
            
            CGMutablePathRef path = CreateRoundRectPath(context, fillBounds);
            CGContextSetRGBFillColor(context, 0.7, 0.7, 0.7, 1.0);
            CGContextFillPath(context);
            CGPathRelease(path);
            
            CGContextSetShadow(context, CGSizeMake(0, 0), 0);
            [self drawPictureStroke:context :bounds];
        }
    }
    
    CGFloat text_height = bounds.size.height * 0.75;
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    NSString* image_count_string = [NSString stringWithFormat:@"%d", self.imageCount];
    UIFont* font = [UIFont fontWithName:@"American Typewriter" size:text_height];
    [image_count_string drawInRect:bounds withFont:font lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentCenter];
}

@end
